const app = getApp();
const api = require("../../utils/request.js");
import { handleDate } from '../../assets/js/global.js'

Page({
  data: {
    state: '',
    params: {
      "babyBirthday": "",
      "babyGender": "1",
      "expectedDateOfChildbirth": "",
      "inoculationStatus": "0",
      "lastMensesStartDate": "",
      "mensesContinuedDays": "5",
      "mensesCycle": "",
      "openId": ""
    },
    readyForm: {
      // date: handleDate('yyyy-MM-dd', new Date()),
      continue: '',
      priod: ''
    },
    proceedForm: {
      // date: handleDate('yyyy-MM-dd', new Date()),
      continue: '',
      birthDate: ''
    },
    finishForm: {
      babyGender: '',
      babyBirthday: ''
      //babyBirthday: handleDate('yyyy-MM-dd', new Date())
    },
    sexIndex: 0,
    sexArray: [{text: '男宝', id: "1"}, {text: '女宝', id: "2"}],
    continueIndex: 2,
    continueArray: [3, 4, 5, 6, 7, 8],
    periodIndex: 10,
    periodArray: [20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36]
  },
  onLoad: function (options) {
    // this.setData({
    //   "params.openId": app.get_open_id()
    // });
  },
  onShow: function () {
  },
  onUnload: function () {

  },
  changeState (e) {
    // console.log(e)
    this.setData({state: e.detail})
  },
  bindDateChange (e) {
    this.setData({[e.target.id]: e.detail.value})
  },
  bindPickerChange (e) {
    console.log(e)
    this.setData({ 
      // [e.target.id]: this.data[e.target.dataset.array][Number(e.detail.value)], 
      // [e.target.dataset.field]: Number(e.detail.value)
      // "params.babyGender": e.detail.value + 1,
      sexIndex: e.detail.value,
      [e.target.id]: this.data[e.target.dataset.array][e.detail.value],
      [e.target.dataset.field]: Number(e.detail.value) + 1
    })
    console.log(this.data.params.babyGender)
  },

  /**
   * 预产期计算法：最后那次月经月数上加9或减3，日子加7。如果用农历计算，月的计算方法相同，日子上改为加15。 举例：最后一次月经是2014年2月1日。日期 1 + 7=8月份 2 + 9=11预产期为： 11月8日（2014年）最后一次月经是2014年4月15日日期 15 + 7=22月份 4 - 3=1预产期为： 1月22日（2015年）
   * 
   */

  calcBabyBirth () {
    const lastDate = this.data.params.lastMensesStartDate //末次月经日期
    const avZhouqi = 28;//平均月经周期
    if (!lastDate) {
      wx.showToast({
        title: '请先选择末次月经时间',
        icon:'none'
      })
      return false
    }
    let tail = new Date(lastDate).getTime();////你的末次月经时间的毫秒表示
    //将末次月经第一天+平均月经周期天数-14天+266天
    //将末次月经第一天+平均月经周期天数+252天
    let ycq = new Date(tail + (28 + 252) * 86400000);//预产期日期
    let year = ycq.getFullYear()
    let month = this.transDate(ycq.getMonth() + 1)
    let day = this.transDate(ycq.getDate())
    this.setData({
      "params.expectedDateOfChildbirth": year + '-' + month + '-' + day
    })
  },
  // 不足10补0
  transDate (num) {
    if (num > 10) {
      return num
    } else {
      return '0' + num
    }
  },
  // 怀孕中取消计算
  hanleCancel () {
    this.setData({
      "params.expectedDateOfChildbirth": ''
    })
  },

  toIndex () {    
    let userInfo = wx.getStorageSync('userInfo')
    if (userInfo) {
      this.setData({
        "params.openId": userInfo.openId
      })
    }
    this.setData({
      "params.inoculationStatus": this.data.state
    })
    // if (this.data.params.inoculationStatus === '1' || this.data.params.inoculationStatus === '2') {
    //   this.setData({
    //     "params.babyGender": "",
    //     "params.babyBirthday": ""
    //   })
    // } else if (this.data.params.inoculationStatus === '3') {
    //   this.setData({
    //     "params.mensesContinuedDays": "",
    //     "params.mensesCycle": ""
    //   })
    // }

    let state = this.data.state
    let params = this.data.params
    if (state == 1) { //备孕中
      if (!params.lastMensesStartDate || !params.mensesContinuedDays || !params.mensesCycle) {
        wx.showToast({
          title: '请先补全信息',
          icon:'none'
        })
        return false
      }
    } else if (state == 2){ // 怀孕中
      if (!params.lastMensesStartDate || !params.mensesContinuedDays || !params.expectedDateOfChildbirth) {
        wx.showToast({
          title: '请先补全信息',
          icon: 'none'
        })
        return false
      }
    } else {
      if (!params.babyBirthday || !params.babyGender) {
        wx.showToast({
          title: '请先补全信息',
          icon: 'none'
        })
        return false
      }
    }

    console.log(this.data.params)
    api._post("/woman/api/user/setStatus", this.data.params).then(res => {
      if(res.code !== '0') {
        app.toast(res.msg)
        return false
      }
      let uinfo = wx.getStorageSync('userInfo')
      uinfo.inoculationStatus = this.data.params.inoculationStatus
      wx.setStorageSync('userInfo', uinfo)
      wx.setStorageSync('inoculationStatus', this.data.params.inoculationStatus);
      wx.navigateTo({
        url: `/pages/index/index?id=${this.data.params.inoculationStatus}`,
      })
    });
  }
})